package com.fractalwrench.harmonybootstrap.sample;

import com.beardedhen.harmonybootstrap.BootstrapProgressBar;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;

import java.security.SecureRandom;

public class BootstrapProgressBarExample extends AbilitySlice implements Component.ClickedListener{

    private Button btn_default,btn_size_change,btn_changeColor_example,btn_striped;
    private BootstrapProgressBar bootStrapProgressBar,bootstrapProgressBarSizeChange,bootstrapProgressBarChangeColor, bootstrapProgressBarStriped;
    private SecureRandom random;

    private DefaultBootstrapSize size = DefaultBootstrapSize.MD;

    private void setShapeElement(Button b) {
        b.setBackground(new UiUtil(getApplicationContext()).getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_example_bootstrap_progressbar, null, false);
        btn_default = (Button) rootLayout.findComponentById(ResourceTable.Id_btnDefault);
        setShapeElement(btn_default);
        btn_size_change= (Button) rootLayout.findComponentById(ResourceTable.Id_example_size_change_btn);
        setShapeElement(btn_size_change);
        btn_changeColor_example=(Button) rootLayout.findComponentById(ResourceTable.Id_example_progress_change_color_btn);
        setShapeElement(btn_changeColor_example);
        btn_striped =(Button) rootLayout.findComponentById(ResourceTable.Id_btnStriped);
        setShapeElement(btn_striped);
        bootStrapProgressBar= (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_defaultProgressBar);
        bootstrapProgressBarSizeChange= (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_example_size_change);
        bootstrapProgressBarChangeColor= (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_example_progress_change);
        bootstrapProgressBarStriped = (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_stripedProgressBar);

        btn_default.setClickedListener(this);
        btn_size_change.setClickedListener(this);
        btn_changeColor_example.setClickedListener(this);
        btn_striped.setClickedListener(this);
        setUIContent(rootLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btnDefault:
                bootStrapProgressBar.setProgress((randomProgress(bootStrapProgressBar.getProgress(), 100)));
                break;

            case ResourceTable.Id_example_size_change_btn:
                onSizeExampleChangeClicked();
                break;

            case ResourceTable.Id_example_progress_change_color_btn:
                onAlterProgressBarColor();
                break;

            case ResourceTable.Id_btnStriped:
                bootstrapProgressBarStriped.setProgress(randomProgress(bootstrapProgressBarStriped.getProgress(), 200));

            default:
                break;
        }
    }

    void onSizeExampleChangeClicked() {
        switch (size) {
            case XS:
                size = DefaultBootstrapSize.SM;
                break;
            case SM:
                size = DefaultBootstrapSize.MD;
                break;
            case MD:
                size = DefaultBootstrapSize.LG;
                break;
            case LG:
                size = DefaultBootstrapSize.XL;
                break;
            case XL:
                size = DefaultBootstrapSize.XS;
                break;
        }
        bootstrapProgressBarSizeChange.setBootstrapSize(size);
    }

    void onAlterProgressBarColor() {
        switch ((DefaultBootstrapBrand) bootstrapProgressBarChangeColor.getBootstrapBrand()) {
            case PRIMARY:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.SUCCESS);
                break;
            case SUCCESS:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.INFO);
                break;
            case INFO:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.WARNING);
                break;
            case WARNING:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.DANGER);
                break;
            case DANGER:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.SECONDARY);
                break;
            case SECONDARY:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.REGULAR);
            case REGULAR:
                bootstrapProgressBarChangeColor.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                break;
        }}

    private int randomProgress(int currentProgress, int maxProgress) {
        if (random == null) {
            random = new SecureRandom();
        }

        int prog = currentProgress + random.nextInt(20);

        if (prog > maxProgress) {
            prog -= maxProgress;
        }

        return prog;
    }
}
